package itstudio.instructor.adapter;

import itstudio.app.wq.R;
import itstudio.instructor.config.Config;
import itstudio.instructor.entity.User;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

public class MyFansAdapter extends BaseAdapter {
	private final class ViewHolder {
		ImageView itemIcon;
		TextView itemName;
		TextView itemIntro;
	}

	private List<User> userList = new ArrayList<User>();;
	private User user;
	DisplayImageOptions options;
	private LayoutInflater mInflater;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	// 单行的布局
	private Context context;
	public MyFansAdapter(Context context, List<User> userList,
			DisplayImageOptions options) {

		if(userList!=null){
			this.userList = userList;
		}
		this.context = context;
		this.options = options;
		this.mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return userList ==null ? 0:userList.size();
	}
	public void appendToList(List<User> addList) {

		if (addList == null) {
			return;
		}
		if(userList==null){
			userList = new ArrayList<User>();
		}
		userList.addAll(addList);
		notifyDataSetChanged();
	}
	
	public int appendData(List<User> addList,boolean refesh) {
		int result =0;
		if (addList == null || addList.size()==0) {
			result = userList.size()/10;
			if(userList.size()%10!=0){
				result++;
			}
			return result;
		}
		// 首次加载
		if(userList.size()==0){
			userList.addAll(addList);//在加载新的
			notifyDataSetChanged();
			return 1; //当前是第一页
		}
		// 有新的数据
		if(!userList.get(0).getId().equals(addList.get(0).getId())){
			
			if(refesh){
				userList.clear();  //  先清空
				userList.addAll(addList);//在加载新的
				notifyDataSetChanged();
				return 1;
			}else{
				userList.addAll(addList);//直接追加
				notifyDataSetChanged();
				result = userList.size()/10;
				if(userList.size()%10!=0){
					result++;
				}
				return result;
			}
		}
		// 没有新的数据
		result = userList.size()/10;
		if(userList.size()%10!=0){
			result++;
		}
		return result;
	
	}
	@Override
	public Object getItem(int position) {
		
		return userList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		user = userList.get(position);
		if (convertView == null) {
			holder = new ViewHolder();
			// 使用自定义的list_items作为layout
			convertView = mInflater.inflate(R.layout.listitem_fans,
					null);

			// 使用减少findciew的次数
			holder.itemIcon = (ImageView) convertView
					.findViewById(R.id.myfans_user_image);
			holder.itemName = (TextView) convertView
					.findViewById(R.id.myfans_name);
			holder.itemIntro = (TextView) convertView
					.findViewById(R.id.myfans_intro);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.itemName.setText(user.getName());  //空指针异常原因是  在if前忘写user = userList.get(position);
		holder.itemIntro.setText(user.getIntro());
		// 给ImageView设置路径Tag,这是异步加载图片的小技巧
		convertView.setTag(holder);
		imageLoader.displayImage(Config.HEAD_URL+user.getHeadUrl(), holder.itemIcon, options,
				new SimpleImageLoadingListener() {
					@Override
					public void onLoadingStarted(String imageUri, View view) {
						// holder.progressBar.setProgress(0);
						// holder.progressBar.setVisibility(View.VISIBLE);
					}

					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason) {
						// holder.progressBar.setVisibility(View.GONE);
					}

					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {
						// holder.progressBar.setVisibility(View.GONE);
					}
				}, new ImageLoadingProgressListener() {
					@Override
					public void onProgressUpdate(String imageUri, View view,
							int current, int total) {
						// holder.progressBar.setProgress(Math.round(100.0f *
						// current / total));
					}
				});
		
		return convertView;
	}

}
